//
//  ViewController.swift
//  Demo-8 识别旋转手势
//
//  Created by quanhao huang on 2019/9/29.
//  Copyright © 2019 demo. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var rotationView: UIView!
    @IBOutlet weak var rotationLabel: UILabel!
    
    //旋转角度
    var angle:Float = 0.0
    
    var angleDegrees : Float {
        get {
            return self.angle*180.0 / Float(Double.pi).truncatingRemainder(dividingBy:360.0)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //旋转手势
        let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotated(rotationGesture:)))
        
        self.rotationView.isUserInteractionEnabled = true
        self.rotationView.addGestureRecognizer(rotationGesture)
        
        self.rotationLabel.text = "\(self.angleDegrees)"
    }

    @objc func rotated(rotationGesture:UIRotationGestureRecognizer) {
        switch rotationGesture.state {
        case .changed:
            self.angle += Float(rotationGesture.rotation)
            rotationGesture.rotation = 0.0
            self.rotationView.transform = CGAffineTransform(rotationAngle: CGFloat(self.angle))
            break
        default:
            break
        }
        self.rotationLabel.text = "\(self.angleDegrees)"
    }

}

